package vista;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Image;

import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.Toolkit;

import javax.swing.JLabel;

import controlador.Controlador;
import controlador.IngresarCtrl;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

public class Ingresar extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private final JPanel contentPanel = new JPanel();
	
	private Controlador controlador;
	private JTextField txtCantidad;
	private JTextField txtCostoTotal;
	private JTextField txtObs;
	private JTextField txtCostoUnidad;
	private IngresarCtrl ingresarCtrl;
	@SuppressWarnings("rawtypes")
	private JComboBox cbxItem;
	@SuppressWarnings("rawtypes")
	private JComboBox cbxCentroCosto;
	@SuppressWarnings("rawtypes")
	private JComboBox cbxItemId;
	private JLabel lblFoto;
	private JTextField txtEnStock;

	/**
	 * Create the dialog.
	 */
	@SuppressWarnings("rawtypes")
	public Ingresar() {
		setResizable(false);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowOpened(WindowEvent arg0) {
//				ingresarCtrl = controlador.getIngresarCtrl();
//				ingresarCtrl.actualizarComboBox();
			}
		});
		setTitle("Ingresar");
		setIconImage(Toolkit.getDefaultToolkit().getImage(Ingresar.class.getResource("/modelo/imagenes/cute_pictures_2921cd4.jpg")));
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblItem = new JLabel("Item");
		lblItem.setBounds(10, 47, 80, 14);
		contentPanel.add(lblItem);
		
		JLabel lblNewLabel = new JLabel("Cantidad");
		lblNewLabel.setBounds(10, 97, 80, 14);
		contentPanel.add(lblNewLabel);
		
		JLabel lblCosto = new JLabel("Costo Total");
		lblCosto.setBounds(10, 122, 80, 14);
		contentPanel.add(lblCosto);
		
		JLabel lblIngresarAlStock = new JLabel("Ingresar stock");
		lblIngresarAlStock.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblIngresarAlStock.setBounds(10, 11, 203, 25);
		contentPanel.add(lblIngresarAlStock);
		
		JLabel lblCentroDeCosto = new JLabel("Centro de Costo");
		lblCentroDeCosto.setBounds(223, 47, 80, 14);
//		contentPanel.add(lblCentroDeCosto);
		
		JLabel lblCostoUnidad = new JLabel("Costo Unidad");
		lblCostoUnidad.setBounds(10, 204, 80, 14);
		contentPanel.add(lblCostoUnidad);
		
		JLabel label = new JLabel("Observaciones");
		label.setBounds(10, 159, 106, 14);
		contentPanel.add(label);
		
		cbxItem = new JComboBox();
		cbxItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (!(cbxItem.getSelectedIndex() == 0)) {
					cbxItemId.setSelectedIndex(cbxItem.getSelectedIndex());
					controlador.getIngresarCtrl().buscarImagen(cbxItemId.getSelectedItem().toString(),
							lblFoto.getHeight(), lblFoto.getWidth());
					txtEnStock.setText(controlador.getDespacharCtrl()
							.buscarEnStock(
									cbxItemId.getSelectedItem().toString()));
				}else{
					borrarCampos();
				}
			}
		});
		cbxItem.setBounds(10, 67, 388, 20);
		contentPanel.add(cbxItem);
		
		cbxCentroCosto = new JComboBox();
		cbxCentroCosto.setBounds(313, 44, 85, 20);
//		contentPanel.add(cbxCentroCosto);
		
		txtCantidad = new JTextField();
		txtCantidad.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				calcularUnidad();
			}
		});
		txtCantidad.setBounds(108, 94, 86, 20);
		contentPanel.add(txtCantidad);
		txtCantidad.setColumns(10);
		
		txtCostoTotal = new JTextField();
		txtCostoTotal.addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent arg0) {
				calcularUnidad();
			}
		});
		txtCostoTotal.setBounds(108, 119, 86, 20);
		contentPanel.add(txtCostoTotal);
		txtCostoTotal.setColumns(10);
		
		txtObs = new JTextField();
		txtObs.setBounds(10, 173, 293, 20);
		contentPanel.add(txtObs);
		txtObs.setColumns(10);
		
		txtCostoUnidad = new JTextField();
		txtCostoUnidad.setEditable(false);
		txtCostoUnidad.setBounds(108, 201, 86, 20);
		contentPanel.add(txtCostoUnidad);
		txtCostoUnidad.setColumns(10);
		
		lblFoto = new JLabel("");
		lblFoto.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		lblFoto.setBounds(313, 101, 86, 98);
		contentPanel.add(lblFoto);
		
		JLabel label_1 = new JLabel("En Stock");
		label_1.setBounds(223, 94, 80, 14);
		contentPanel.add(label_1);
		
		txtEnStock = new JTextField();
		txtEnStock.setEditable(false);
		txtEnStock.setColumns(10);
		txtEnStock.setBounds(217, 116, 86, 20);
		contentPanel.add(txtEnStock);
		
		cbxItemId = new JComboBox();
		cbxItemId.setBounds(62, 44, 80, 20);
//		contentPanel.add(cbxItemId);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			
			JButton btnNuevoItem = new JButton("Nuevo Item");
			btnNuevoItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					controlador.mostrarVentanaItem();
				}
			});
			buttonPane.add(btnNuevoItem);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						
						ingresarCtrl = controlador.getIngresarCtrl();
						
						ingresarCtrl.setTxtCantidad(txtCantidad.getText());
						ingresarCtrl.setTxtCentroCosto(cbxCentroCosto.getSelectedItem().toString());
						ingresarCtrl.setTxtCostoTotal(txtCostoTotal.getText());
						ingresarCtrl.setTxtCostoUnidad(txtCostoUnidad.getText());
						ingresarCtrl.setTxtItem(cbxItemId.getSelectedItem().toString());						
						ingresarCtrl.setTxtObs(txtObs.getText());
						
						if (Integer.parseInt(txtCantidad.getText())>0) {
							if (ingresarCtrl.ingresarStock()) {
								cerrarVentana();
							}							
						} else {
							JOptionPane.showMessageDialog(null, "La cantidad ingresada no es valida", "Advertencia", JOptionPane.WARNING_MESSAGE);
						}
						
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						cerrarVentana();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
	
	public void setControlador(Controlador miControlador) {
		this.controlador = miControlador;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setModeloComboboxItem(DefaultComboBoxModel x){
		cbxItem.setModel(x);
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setModeloComboboxCentroCosto(DefaultComboBoxModel x){
		cbxCentroCosto.setModel(x);
	}
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void setModeloCbxItemId(DefaultComboBoxModel x){
		cbxItemId.setModel(x);
	}

	private void calcularUnidad() {
		
		int a=0, b=0, c=0;
		
		try {
			a = Integer.parseInt(txtCostoTotal.getText());
			b = Integer.parseInt(txtCantidad.getText());
			if(b!=0)			
				c = a / b;
		} catch (Exception e) {

		}		
		txtCostoUnidad.setText(""+c);
	}
	public void cambiarImagen(Image icon){
		lblFoto.setIcon(new ImageIcon(icon));
	}
	public void borrarCampos(){
		lblFoto.setIcon(null);
		txtCantidad.setText("");
		txtCostoTotal.setText("");
		txtCostoUnidad.setText("");
		txtObs.setText("");		
		txtEnStock.setText("");
	}
	
	private void cerrarVentana() {
		controlador.ocultarVentanaIngresar();
		borrarCampos();
	}
}
